<?php

abstract class table
{
	protected $table_name = '';
	protected $table_key = '';

	protected $error = '';

	public function __construct($table_name, $table_key='id')
	{
		$this->table_name = $table_name;
		$this->table_key = $table_key;
	}


	/*
	绑定一个数据源， GET, POST, 或者一个数组, 对象 
	*/
	public function bind($data)
	{
		if (!is_object($data) && !is_array($data))
		{
			$this->error = '参数错误, 绑定数据源必须为对象或数组';
			return false;
		}

		if (is_object($data)) $data = get_object_vars($data);

		$properties  = get_object_vars($this);

		foreach ($properties as $key => $value)
		{
			if (isset($data[$key]))
			{
				$val = $data[$key];
				// $val = mysql_escape_string($data[$key]);
				$this->$key = $val;
			}
		}

		return true;
	}

	// 加载记录
	public function load($id = null)
	{
		$table_key = $this->table_key;

		$id = (is_null($id)) ? $this->$table_key : $id;

		if ($id === null)
		{
			$this->error = '参数错误, 请指定要加载记录的编号';
			return false;
		}

		$db = bone::get_db();
		$row = $db->get_object('SELECT * FROM `'.$this->table_name.'` WHERE `'.$this->table_key.'` = \''.$id.'\'');

		if ($db->get_error_num())
		{
			$this->error = $db->get_error_num().': '.$db->get_error_msg();
			return false;
		}

		if (empty($row))
		{
			$this->error = '不存在指定编号的记录';
			return false;
		}

		return $this->bind($row);
	}

	
	// 加载记录
	public function load_by($key, $value = null)
	{
		$db = bone::get_db();
		if($value)
		{
			$row = $db->get_object('SELECT * FROM `'.$this->table_name.'` WHERE `'.$key.'` = \''.$value.'\'');
		}
		else
		{
			$row = $db->get_object('SELECT * FROM `'.$this->table_name.'` WHERE '.$key);
		}

		if ($db->get_error_num())
		{
			$this->error = $db->get_error_num().': '.$db->get_error_msg();
			return false;
		}

		if (empty($row))
		{
			$this->error = '不存在指定条件的记录';
			return false;
		}

		return $this->bind($row);
	}



	// 保存数据 $bUpdateNull 是否更新空记录
	public function save($update_null = false)
	{
		if (!$this->check())
		{
			if(!$this->error) $this->error = '数据检查错误';
			return false;
		}

		$db = bone::get_db();

		$table_key = $this->table_key;
		if ($this->$table_key)
		{
			$stored = $db->update_object($this->table_name, $this, $this->table_key, $update_null);
		}
		else
		{
			$stored = $db->insert_object($this->table_name, $this, $this->table_key);

			$table_key = $this->table_key;
			$this->$table_key = $db->get_insert_id();
		}

		if (!$stored)
		{
			$this->error = $db->get_error_num().': '.$db->get_error_msg();
			return false;
		}

		return true;
	}


	public function delete($id = null)
	{
		$table_key = $this->table_key;
		$id = (is_null($id)) ? $this->$table_key : $id;
		if ($id === null)
		{
			$this->error = '参数错误, 请指定要删除记录的编号';
			return false;
		}

		$db = bone::get_db();
		$db->execute('DELETE FROM `'.$this->table_name.'` WHERE `'.$this->table_key.'`=\''.$id.'\'');

		if ($db->get_error_num())
		{
			$this->error = $db->get_error_num().': '.$db->get_error_msg();
			return false;
		}

		return true;
	}


	// 检查数据是否舍法， 子类须重写本方法。 检查提交的数据。
	public function check()
	{
		return true;
	}


	public function set_error($error){$this->error=$error;}
	public function get_error(){return $this->error;}

}